package com.tiepbm.cucphamgiadinh.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tiepbm.cucphamgiadinh.adapter.MyPageAdapter;
import com.tiepbm.cucphamgiadinh.fragment.FragmentDetail;
import com.tiepbm.cucphamgiadinh.R;
import com.tiepbm.cucphamgiadinh.util.MyPreferenceManager;

public class ReadActivity extends FragmentActivity implements IScreen {

	private int pos = 1;
	private int maxchapter;
	private ViewPager pager;
	private MyPageAdapter pageAdapter;
	private TextView tvChaper;
	private Button btSizeText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_read2);
		initView();
		implementListener();
		fillData();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		pager = (ViewPager) findViewById(R.id.pager);
		tvChaper = (TextView) findViewById(R.id.title);
		btSizeText = (Button) findViewById(R.id.button1);
	}

	@Override
	public void implementListener() {
		// TODO Auto-generated method stub
		btSizeText.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentDetail fr = (FragmentDetail)pageAdapter.getItem(pos);
				fr.changeTextSize();
			}
		});
	}

	@Override
	public void fillData() {
		// TODO Auto-generated method stub
		Bundle rb = this.getIntent().getExtras();
		pos = rb.getInt("chap");
		System.out.println("POS: " + pos);
		maxchapter = MyPreferenceManager.getInstance(this).getChapterMax();
		
		List<Fragment> fragments = getFragments();
		pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
		pager.setAdapter(pageAdapter);
		pager.setCurrentItem(pos);
		tvChaper.setText("Chương "+pos);
		pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				MyPreferenceManager mypreference = MyPreferenceManager
						.getInstance(ReadActivity.this);
				mypreference.setChaper(arg0);
				pos = arg0;
				tvChaper.setText("Chương "+pos);

			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private List<Fragment> getFragments() {

		List<Fragment> fList = new ArrayList<Fragment>();
		int i;
		for (i = 0; i < maxchapter; i++) {
			fList.add(FragmentDetail.newInstance(i));
		}
		return fList;

	}

}
